package com.yc.c118s3plyqnzf.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.google.gson.Gson;
import com.yc.c118s3plyqnzf.entity.Type;
import com.yc.c118s3plyqnzf.mapper.TypeMapper;
import com.yc.c118s3plyqnzf.service.ITypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lcx
 * @since 2022-11-28
 */
@Service
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type> implements ITypeService {

    // 基于对字符串类型数据操作的模板
    @Resource
    StringRedisTemplate stringRedisTemplate;

    @Override
    public List<Type> queryAll() {
        // 设计键值：QnzfTypes
        boolean has = stringRedisTemplate.hasKey("QnzfTypes");
        if (has){
            System.out.println("Redis 缓存数据....");
            // 获取redis 中的 type list 集合
            List<String> qnzfTypes = stringRedisTemplate.opsForList().range("QnzfTypes", 0, -1);
            // 将list 中的 json 转成 Type
            List<Type> list = qnzfTypes.stream().map(json->{
                Type type = new Gson().fromJson(json, Type.class);
                return type;
            }).collect(Collectors.toList());
            return list;
        } else {
            List<Type> list = this.list();
            // 将查询结果缓存
            for (Type type : list){
                String json = new Gson().toJson(type);
                stringRedisTemplate.opsForList().rightPush("QnzfTypes", json);
            }
            return list;
        }
    }

    @Override
    public boolean save(Type entity) {
        stringRedisTemplate.delete("QnzfTypes");
        return super.save(entity);
    }

    @Override
    public boolean saveBatch(Collection<Type> entityList) {
        stringRedisTemplate.delete("QnzfTypes");
        return super.saveBatch(entityList);
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<Type> entityList) {
        stringRedisTemplate.delete("QnzfTypes");
        return super.saveOrUpdateBatch(entityList);
    }

    @Override
    public boolean updateById(Type entity) {
        stringRedisTemplate.delete("QnzfTypes");
        return super.updateById(entity);
    }

    @Override
    public boolean update(Wrapper<Type> updateWrapper) {
        stringRedisTemplate.delete("QnzfTypes");
        return super.update(updateWrapper);
    }

    @Override
    public boolean update(Type entity, Wrapper<Type> updateWrapper) {
        stringRedisTemplate.delete("QnzfTypes");
        return super.update(entity, updateWrapper);
    }

    @Override
    public boolean updateBatchById(Collection<Type> entityList) {
        stringRedisTemplate.delete("QnzfTypes");
        return super.updateBatchById(entityList);
    }

    @Override
    public UpdateChainWrapper<Type> update() {
        stringRedisTemplate.delete("QnzfTypes");
        return super.update();
    }

    @Override
    public boolean saveOrUpdate(Type entity, Wrapper<Type> updateWrapper) {
        stringRedisTemplate.delete("QnzfTypes");
        return super.saveOrUpdate(entity, updateWrapper);
    }
}
